package edu.sias.service;

import edu.sias.dao.SearchDao;
import edu.sias.pojo.Post;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//检索数据的业务层
public class SearchService {
    //创建searchDao
    SearchDao searchDao = new SearchDao();

    /**
     * 根据文章id查询文章信息
     * @param id 文章的唯一标识
     * @return 对应的post对象
     */
    public Post findPostById(int id){
        Post post = null;
        try {
            post = searchDao.queryPostById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return post;
    }



    //检索数据的业务逻辑
    public List<Post> getMsgInfo(String keyMsg){
        //业务层 调用 数据层
        SearchDao searchDao = new SearchDao();
        List<Post> posts = null;
        try {
            posts = searchDao.findMsg(keyMsg);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //假设，对原始数据，按照点击量进行排序
        Collections.sort(posts, new Comparator<Post>() {
            @Override
            public int compare(Post o1, Post o2) {
                return o2.getClick().compareTo(o1.getClick());
            }
        });
        //返回处理好的数据
        return posts;
    }

    //业务层中关于根据类型查找文章信息
    public List<Post> findPostByCategotyId(int id) {
        List<Post> list = null;
        try {
            list = searchDao.queryPostByCategoryId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Collections.sort(list, new Comparator<Post>() {
            @Override
            public int compare(Post o1, Post o2) {
                return o2.getClick().compareTo(o1.getClick());
            }
        });

        return list;
    }
}
